<?php

namespace app\web\model;
use think\Model;
use think\Db;
use app\common\model\Member;
class OrderModel extends Model {
	protected $name = 'mall_order';
	protected $member_model;

	// 开启自动写入时间戳
	protected $autoWriteTimestamp = false;
	protected function _initialize() {
		parent::_initialize();
	}

	
	const TO_PAY = 1;
	const PIN_TUAN = 2;
	const PAID_TO_SEND =3;
	const SENT_TO_RECEIVE =4;
	const RECEIVED_FINISH = 5;
	const SYSTEM_CLOSE = 6;
	const PAID_TO_FETCH = 7;
	const NO_EXPRESS_DELIVERY = 8;
	const CUSTOMER_FETCH_FINISH =9;
	const ADMIN_CLOSE = 10;
	const CUSTOMER_CLOSE = 11;
	
	public static function getText($status) {
		$status = intval($status);
		switch ($status) {
			case self::TO_PAY:
				return '未付款';
			case self::PAID_TO_SEND:
				return '待发货';
			case self::PAID_TO_FETCH:
				return '待消费';
			case self::PIN_TUAN:
				return '拼团中';
			case self::SENT_TO_RECEIVE:
				return '待收货';
			case self::RECEIVED_FINISH:
			case self::CUSTOMER_FETCH_FINISH:
				return '交易完成';
			case self::SYSTEM_CLOSE:
			case self::ADMIN_CLOSE:
			case self::CUSTOMER_CLOSE:
				return '已关闭';
			case self::NO_EXPRESS_DELIVERY:
				return '物流不达';
			default:
				return '未知状态';
		}
	}
	
	

	/**
	 * 订单收货完成分佣奖励
	 */
	function order_complete($order_id) {
		$this -> member_model = new Member();
		$order = $this -> find($order_id);
		$distrbution = getDisbution();
		//分销设置
		$payfee = $distrbution['payfee'];
		//支付费率
		$postfee = $order['postage'];
		//邮费
		$balance = $order['total_price'] - $payfee * $order['total_price']/100-$postfee;
		//商家扣除支付费率商家得到的金额
		$allAgentBalance = $order['pt_money'];
		//合伙人自己
		$agentcommission = $distrbution['agentcommission'];
		//总代理订单提成比例
		$commission1 = $distrbution['commission1'];
		//1级订单提成比例
		$commission2 = $distrbution['commission2'];
		//2级订单提成比例
		$cityagentcommission = $distrbution['cityagentcommission'];
		//城市运营商提成
		if (!empty($order['level1_money']) || !empty($order['level2_money']) || !empty($order['level3_money'])) {
			$nickname = db('member') -> where('id', $order['user_id']) -> value("mobile");
			$this -> member_model -> mc_fanxian($nickname, $order);
			//向上级返现
		}
		if ($allAgentBalance > 0) {//记录合伙人资金变化
			$this -> member_model -> insbalchuser($order['pt_id'], $order['sn'] . '订单确认收货', $allAgentBalance, 1, $order['id']);
		}
		if ($balance > 0) {//商家资金增加
			$this -> member_model -> insbalch($order['agent_id'], $order['sn'] . '订单确认收货', $balance, 1, $order['id']);
			//商家增加余额收入
			if (!empty($order['need_score']) && $order['need_score'] > 0) {//疯狂抢商品，商家增加积分余额
				$this -> member_model -> insscoch($order['agent_id'], $order['sn'] . '订单确认收货', $order['need_score'], 1, $order['id']);
			}
			if (!empty($order['give_score']) && $order['give_score'] > 0) {//送积分商品，商家减少积分余额
				$this -> member_model -> insscoch($order['agent_id'], $order['sn'] . '订单确认收货', -$order['give_score'], 1, $order['id']);
			}
		}

	}

}
